<template>
  <view class="details-container">
    <scroll-view scroll-y class="scroll-area">
      <view class="content">
        <view v-if="loading" class="loading-container">
          <text class="loading-text">加载中...</text>
        </view>

        <template v-else>
          <view class="card status-card">
            <view class="status-left">
              <uv-icon name="clock" :size="22" :color="'#FF6B35'" />
              <text class="status-text">{{ formatOll(orderDetail.status) }}</text>
            </view>
          </view>

          <view class="card address-card">
            <view class="addr-row">
              <uv-icon name="map-fill" :size="22" :color="'#FF6B35'" />
              <view class="addr-info">
                <text class="addr-label">派送地址</text>
                <text class="addr-text">{{ orderDetail.address_detail || '暂无地址信息' }}</text>
              </view>
            </view>
          </view>

          <view class="card product-card">
            <view class="product-left">
              <view class="oil-box">
                <image :src="orderDetail.goods_image" mode="aspectFill" style="width: 100%; height: 100%;" />
              </view>
            </view>
            <view class="product-main">
              <view class="product-title">{{ orderDetail.goods_name || '0#号汽油' }}</view>
              <view class="product-meta">用油量：{{ orderDetail.usage || 1 }}升</view>
            </view>
            <view class="product-price">¥{{ orderDetail.goods_price || '4080.05' }}</view>
          </view>

          <view class="card time-card">
            <view class="time-row">
              <text class="time-label">下单时间：</text>
              <text class="time-value">{{
                formatDateTime(orderDetail.order_time) || '2023.10.10 12:00'
              }}</text>
            </view>
            <view class="divider"></view>
            <view class="time-row">
              <text class="time-label">用油时间：</text>
              <text class="time-value">{{
                formatDateTime(orderDetail.use_time) || '2023.10.10 12:00'
              }}</text>
            </view>
          </view>
        </template>
      </view>
    </scroll-view>

    <view class="footer">
      <uv-button text="接单" size="large" :custom-style="primaryBtn" @click="accept" />
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRequest } from '@/api'
import { UserInfoManager } from '@/utils/userInfo'
import { formatDateTime } from '@/utils/dateTime'
import { onLoad } from '@dcloudio/uni-app'
const { API_JIDAN_GET, API_JIDAN_DETAIL_GET } = useRequest()

const orderId = ref('')
const orderDetail = ref<any>({})
const loading = ref(false)
const primaryBtn = {
  width: '100%',
  height: '88rpx',
  borderRadius: '48rpx',
  backgroundColor: '#FF6B35',
  color: '#fff',
  fontSize: '32rpx',
  fontWeight: '600'
}


onLoad((options: Record<string, any>) => {
  const id = options?.id ?? options?.orderId
  if (id) {
    try {
      orderId.value = decodeURIComponent(String(id))
    } catch {
      orderId.value = String(id)
    }
  }
})
const getOrderDetail = async () => {
  if (!orderId.value) {
    uni.showToast({
      title: '订单ID不能为空',
      icon: 'none'
    })
    return
  }
  loading.value = true
  try {
    const params = {
      id: orderId.value,
      isdriver: UserInfoManager.lastrole()
    }
    const res = await API_JIDAN_DETAIL_GET(params)
    if (res.code === 1) {
      orderDetail.value = res.data
    }
  } catch (error) {

  } finally {
    loading.value = false
  }
}

const accept = async () => {
  if (!orderId.value) {
    uni.showToast({
      title: '订单ID不能为空',
      icon: 'none'
    })
    return
  }

  try {
    const params = {
      isdriver: UserInfoManager.lastrole(),
      id: orderId.value
    }

    const res = await API_JIDAN_GET(params)
    if (res.code === 1) {
      uni.showToast({ title: '操作成功', icon: 'success' })
      setTimeout(() => {
        // 发送刷新事件
        uni.$emit('refreshOrderList')

        // 延迟返回，确保事件发送成功
        setTimeout(() => {
          uni.navigateBack()
        }, 100)
      }, 500)
    }
  } catch (error) {

  }
}
const formatOll = (status: string) => {
  const statusMap: Record<string, string> = {
    pending: '待接单',
    delivery: '配送中',
    cancelled: '已取消',
    comeon: '加油中',
    pendingpay: '待付款',
    completed: '已完成'
  }

  return statusMap[status] || '未知状态'
}

onMounted(() => {
  getOrderDetail()
})
</script>

<style lang="scss" scoped>
.details-container {
  min-height: 100vh;
  background: #f6f7f9;
  display: flex;
  flex-direction: column;
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 100rpx 0;
}

.loading-text {
  font-size: 28rpx;
  color: #999;
}

/* 将横向内边距放在 .content，保证左右一致 */
.scroll-area {
  flex: 1;
  padding: 20rpx 0;
  padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
}

.content {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
  padding: 0 24rpx;
}

.card {
  background: #fff;
  border-radius: 16rpx;
  box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.06);
  padding: 24rpx;
}

.status-card {
  display: flex;
  align-items: center;
}

.status-left {
  display: flex;
  align-items: center;
  gap: 12rpx;
}

.status-text {
  font-size: 30rpx;
  color: #333;
  font-weight: 600;
}

.address-card .addr-row {
  display: flex;
  align-items: center;
  gap: 16rpx;
}

.addr-info {
  display: flex;
  flex-direction: column;
  gap: 6rpx;
}

.addr-label {
  font-size: 28rpx;
  color: #8c8c8c;
}

.addr-text {
  font-size: 30rpx;
  color: #1f1f1f;
  font-weight: 500;
}

.product-card {
  display: flex;
  align-items: center;
  gap: 16rpx;
}

.oil-box {
  width: 120rpx;
  height: 120rpx;
  border-radius: 16rpx;
  background: #fff5f0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.oil-box .oil-tag {
  margin-top: 8rpx;
  font-size: 32rpx;
  font-weight: 700;
  color: #1f1f1f;
}

.product-main {
  flex: 1;
}

.product-title {
  font-size: 34rpx;
  font-weight: 700;
  color: #1f1f1f;
  margin-bottom: 8rpx;
}

.product-meta {
  font-size: 28rpx;
  color: #8c8c8c;
}

.product-price {
  font-size: 34rpx;
  font-weight: 700;
  color: #ff6b35;
}

.time-card .time-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18rpx 0;
}

.time-card .divider {
  height: 1rpx;
  background: #f0f0f0;
}

.time-label {
  font-size: 28rpx;
  color: #8c8c8c;
}

.time-value {
  font-size: 30rpx;
  color: #1f1f1f;
}

.footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 20rpx 24rpx calc(20rpx + env(safe-area-inset-bottom));
  background: #fff;
  box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.06);
}
</style>
